package frame.utils;

import frame.pojo.Configuration;
import frame.pojo.Mapper;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * FileName: XmlConfigBuilder
 * Author:   xuan zongjun
 * Date:     2020/11/4 12:08
 * Description:
 */


public class XmlConfigBuilder {

    /**
     * 读取xml信息为Configuration对象
     * @param inputStream
     * @return
     */
    public Configuration loadXmlConfig(InputStream inputStream){
        Configuration configuration = new Configuration();
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();
            List<Element> selectNodes = rootElement.selectNodes("//property");

            if(selectNodes != null && selectNodes.size() > 0) {
                for (int i = 0; i < selectNodes.size(); i++) {
                    Element element =  selectNodes.get(i);
                    String name = element.attributeValue("name");
                    String value = element.attributeValue("value");

                    if("driver".equalsIgnoreCase(name)) {
                        configuration.setDriver(value);
                    }
                    if("url".equalsIgnoreCase(name)) {
                        configuration.setUrl(value);
                    }
                    if("username".equalsIgnoreCase(name)) {
                        configuration.setUsername(value);
                    }
                    if("password".equalsIgnoreCase(name)) {
                        configuration.setPassword(value);
                    }
                }
            }

            List<Element> mappers = rootElement.selectNodes("//mapper");

            if(mappers != null && mappers.size() > 0) {
                for (int i = 0; i < mappers.size(); i++) {
                    Element element =  mappers.get(i);
                    String mapperPath = element.attributeValue("resource");
                    loadSqlXmlConfig(configuration,mapperPath);
                }
            }


        } catch (DocumentException e) {
            e.printStackTrace();
        }

        return configuration;
    }

    /**
     * 读取存放sql的xml文件
     * @param mapperPath
     */
    private void loadSqlXmlConfig(Configuration configuration,String mapperPath) {
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(mapperPath);
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(resourceAsStream);
            Element rootElement = document.getRootElement();
            String namespace = rootElement.attributeValue("namespace");
            List<Element> selectNodes = rootElement.selectNodes("//select");
            if(selectNodes != null && selectNodes.size() > 0) {
                for (int i = 0; i < selectNodes.size(); i++) {
                    Element element =  selectNodes.get(i);
                    String id = element.attributeValue("id");
                    String resultType = element.attributeValue("resultType");
                    String sql = element.getText();
                    Mapper mapper = new Mapper();
                    mapper.setSql(sql);
                    mapper.setResultType(resultType);
                    configuration.getMapperMap().put(namespace + "." + id,mapper);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

}
